﻿using FreeSql;
using LdtCommonFreeSql;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NlpApp.Model.RexUninlu;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace NlpTrainApp.Bll.RexUninlu
{
    /// <summary>
    /// uie训练业务类
    /// </summary>
    public class TrainRexUninluBll
    {
        private readonly string baseDataPath = Path.Combine(AppContext.BaseDirectory, "rex", "data", "tc_multi_label");
        private readonly IConfiguration configuration;
        //private readonly ExtractServiceBll extractService;
        private readonly FreesqlDb freesqlDb;
        private readonly HubMessageServer hubMessage;
        private readonly ILogger<TrainRexUninluBll> logger;
        public TrainRexUninluBll(ILogger<TrainRexUninluBll> logger, FreesqlDb freesqlDb, IConfiguration configuration, HubMessageServer hubMessage)
        {
            this.logger = logger;
            this.freesqlDb = freesqlDb;
            this.configuration = configuration;
            this.hubMessage = hubMessage;
            //extractService = extractServiceBll;
            Directory.CreateDirectory(baseDataPath);
        }
        /// <summary>
        /// Creates the train file.
        /// </summary>
        public async Task CreateTrainFile()
        {
            var train = File.ReadAllText(@"\\192.168.1.52\CaseCause\App\train.csv").Split("\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            var valid = File.ReadAllText(@"\\192.168.1.52\CaseCause\App\valid.csv").Split("\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            var train1 = GetMULTICLASSIFYData(train);
            var valid1 = GetMULTICLASSIFYData(valid);
            var s1 = train1.SelectMany(p => p.info_list.SelectMany(a => a.Select(b => b.type))).ToList();
            var s2 = valid1.SelectMany(p => p.info_list.SelectMany(a => a.Select(b => b.type))).ToList();
            s1.AddRange(s2);
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            foreach (var item in s1.Distinct())
            {
                keyValuePairs.Add(item, null);
            }
            train1.ForEach(p =>
            {
                p.schema = keyValuePairs;
            });
            valid1.ForEach(p =>
            {
                p.schema = keyValuePairs;
            });
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
            File.WriteAllText(Path.Combine(baseDataPath, "train.json"), string.Join("\n", train1.Select(p => JsonConvert.SerializeObject(p, jsonSerializerSettings))));
            File.WriteAllText(Path.Combine(baseDataPath, "valid.json"), string.Join("\n", valid1.Select(p => JsonConvert.SerializeObject(p, jsonSerializerSettings))));
        }
        private List<RexUninluModel> GetMULTICLASSIFYData(List<string> data)
        {
            return data.SelectMany(p =>
            {
                var t1 = p.Split("\t");
                var t0 = t1[0].Split(",".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                return t1.Select(a => new RexUninluModel
                {
                    id = LdtCommonUtil.ObjectId.GenerateNewStringId(),
                    text = $"[MULTICLASSIFY]{t1[1]}",
                    info_list = new List<List<RexUninluInfolistItem>>() { t0.Select(b => new RexUninluInfolistItem {
                                 offset=new List<int> {0, 15},
                                   span="[MULTICLASSIFY]",
                                    type=b.Split("_")[1]
                            }).ToList() }
                });
            }
                ).ToList();
        }
    }
}
